package com.laiketui.order.service.impl;

import com.laiketui.common.mapper.MchModelMapper;
import com.laiketui.common.mapper.UserMapper;
import com.laiketui.core.exception.LaiKeAPIException;
import com.laiketui.domain.mch.MchModel;
import com.laiketui.domain.user.User;
import com.laiketui.order.api.dto.OrderParentDTO;
import com.laiketui.order.domain.Order;
import com.laiketui.order.domain.OrderParent;
import com.laiketui.order.entity.agreement.OrderAgreement;
import com.laiketui.order.entity.agreement.PartA;
import com.laiketui.order.entity.agreement.PartB;
import com.laiketui.order.entity.agreement.PartC;
import com.laiketui.order.service.OrderAgreementService;
import com.laiketui.order.service.OrderParentService;
import com.laiketui.order.service.OrderService;
import com.laiketui.order.utils.AmountConverter;
import com.laiketui.order.utils.PriceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.SimpleFormatter;

/**
 * 订单服务协议
 */
@Service
public class OrderAgreementServiceImpl implements OrderAgreementService {

    @Autowired
    private OrderParentService orderParentService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private MchModelMapper mchModelMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public OrderAgreement getOrderAgreementInfo(String orderParentId){
        OrderAgreement orderAgreement = new OrderAgreement();
        PartA partA = new PartA();
        List<PartB> partBList = new ArrayList<>();
        PartC partC = new PartC();
        BigDecimal payPrice = null;
        OrderParent orderParent = orderParentService.getOrderParent(orderParentId);
        if (orderParent == null) {
            throw new LaiKeAPIException("找不到该订单");
        }
        Long payPriceL = orderParent.getPayPrice();
        if(payPriceL == null){
            throw new LaiKeAPIException("母单价格为空");
        }
        User user = new User();
        user.setUser_id(orderParent.getUserId());
        user = userMapper.selectOne(user);
        if(user == null){
            throw new LaiKeAPIException("查无此用户");
        }
        partA.setPhone(user.getMobile());
        partA.setAddress(user.getDetailed_address());
        partA.setMemberName(user.getUser_name());
//        String address = orderParent.getProvice() + orderParent.getCity() + orderParent.getArea() + orderParent.getAddress();
//        partA.setMemberName(orderParent.getName());
//        partA.setAddress(address);
//        partA.setPhone(orderParent.getMobile());
        orderAgreement.setPartA(partA);
        payPrice = PriceUtil.longToBigDecimalPrice(payPriceL);
        String chineseUpperCasePayPrice = AmountConverter.toChineseUpperCase(payPrice);
        orderAgreement.setPayPrice(payPrice);
        orderAgreement.setCaptialPayPrice(chineseUpperCasePayPrice);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String now = simpleDateFormat.format(new Date());
        orderAgreement.setCreateDate(now);
        List<Order> orderList = orderService.findByOrderParentNo(orderParentId);
        orderList.forEach(order -> {
            MchModel mchModel = new MchModel();
            mchModel.setId(order.getMchId());
            mchModel = mchModelMapper.selectOne(mchModel);
            PartB partB = new PartB();
            partB.setFactoryName(mchModel.getName());
            partB.setAddress(mchModel.getAddress());
            partB.setPhone(mchModel.getTel());
            partBList.add(partB);
        });
        orderAgreement.setOrderParentId(orderParentId);
        orderAgreement.setPartB(partBList);
        partC.setCompanyName("深圳市珞珂家居有限公司");
        partC.setAddress("深圳市龙岗区宝龙街道同乐社区黄屋路10号101");
        partC.setPhone("0755-23906666");
        orderAgreement.setPartC(partC);
        return orderAgreement;
    }
}
